<?php
/**
 * 操作记录查询
 * Author: lyun
 * Date: 2018/10/16
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Panx;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Panx\OperateLog\OperateLogIndexRequest;
use App\Models\Admin\CustomerAdmin;
use App\Models\Admin\PanxAdmin;
use App\Models\Admin\StoreAdmin;
use App\Models\Logger\LogType;
use App\Models\Logger\OperateLog;
use App\Services\Utils\Guard;

class OperateLogController extends ApiController
{
    /**
     * 查询记录列表
     * @param OperateLogIndexRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(OperateLogIndexRequest $request)
    {
        $panx_admin = Guard::panxAdmin();
        $this->checkStaffAuth($panx_admin);

        $operate_logs = OperateLog::getOperatorLogs($request);

        return $this->response(compact('operate_logs'));
    }

    /**
     * @return array|\Illuminate\Http\Response
     */
    public function create()
    {
        // 感觉没必要筛选指定 customer/store 下的数据
        // $customer = Customer::select('id', 'name')->get();
        // $store = Customer::select('id', 'name')->get();

        $operatorable_type_admin = $this->groupByOperatorableType();

        $log_types = collect(LogType::FILTER_LOG_MAP)
            ->map(function (string $name, int $id) {return compact('id', 'name');})
            ->values();

        return $this->response(compact('operatorable_type_admin', 'log_types'));
    }

    /**
     *  检测用户是否有操作operate_logs表的权限
     * @param PanxAdmin $admin
     *
     * @throws \Exception
     */
    private function checkStaffAuth(PanxAdmin $admin)
    {
        $is_super_man = $admin->hasRole(PanxAdmin::SUPER_MAN);

        $this->checkIsTrue($is_super_man, trans('admin.is_super_man'));
    }

    /**
     * @return array
     */
    private function groupByOperatorableType(): array
    {
//        $device_account = (new DeviceAccount)->where('enabled', DeviceAccount::IS_ENABLED)
//            ->select('id', 'account as name')->get();

        $panx_admins = (new PanxAdmin)->where('is_deleted', PanxAdmin::IS_NOT_DELETED)
            ->select(['id', 'account as name'])->get();

        $customer_admins = (new CustomerAdmin)->where('is_deleted', CustomerAdmin::IS_NOT_DELETED)
            ->select(['id', 'account as name'])->get();

        $store_admins = (new StoreAdmin)->where('is_deleted', StoreAdmin::IS_NOT_DELETED)
            ->select(['id', 'account as name'])->get();

        return [
            [
                'operatorable_type' => class_basename(PanxAdmin::class),
                'admins'            => $panx_admins,
            ],
            [
                'operatorable_type' => class_basename(CustomerAdmin::class),
                'admins'            => $customer_admins,
            ],
            [
                'operatorable_type' => class_basename(StoreAdmin::class),
                'admins'            => $store_admins,
            ]
        ];
    }
}
